/* 菜单树开始 */
var setting = {
	async : {
		enable : true,
		url : baseUrl + '/cud/queryMenuListWithOutCheckBox.do',
		type : 'get',
		autoParam : ["id=nodeId"]
	},
	data : {
		simpleData : {
			enable : true,
			idKey : 'id',
			pIdKay : 'parentId',
			rootPId : "-1"
		},
		key : {
			name : 'text',
			url : 'noneUrl'
		}
	},
	callback : {
		onClick : zTreeOnClick
	}
};
function zTreeOnClick(event, treeId, treeNode) {
	$("#menu_name").val(treeNode.text);
	$("#menu_url").val(treeNode.url);
	if (treeNode.leaf) {
		$("#menu_leaf").val("是");
	} else {
		$("#menu_leaf").val("否");
	}
	$.post(baseUrl + "/menu/queryMenuRoleList.do", {
				menuId : treeNode.id
			}, function(result) {
				// console.info(result.data);
				var menuRloeArr = result.data;
				var roleNames = "";
				for (var i = 0; i < menuRloeArr.length; i++) {
					var roleName = menuRloeArr[i].ROLE_NAME;
					if (0 != i) {
						roleNames += ",";
					}
					roleNames += roleName;
				}
				$("#menu_ssjs").val(roleNames);
			});
	/* 角色树开始 */
	var setting_role = {
		async : {
			enable : true,
			url : baseUrl + '/role/queryRoleListchecked.do',
			type : 'get',
			autoParam : ["id=nodeId"],
			otherParam : ["menuId", treeNode.id]
		},
		data : {
			simpleData : {
				enable : true,
				idKey : 'id',
				pIdKay : 'parentId',
				rootPId : "-1"
			},
			key : {
				name : 'text',
				url : 'noneUrl',
				checked : 'checked'
			}
		},
		check : {
			enable : true,
			chkStyle : 'checkbox'
		},
		callback : {
			onAsyncSuccess : zTreeOnAsyncSuccess,
			onCheck : zTreeOnCheck
		}
	};
	$.fn.zTree.init($("#eastTree"), setting_role);
	function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
		var eastTreeObj = $.fn.zTree.getZTreeObj("eastTree");
		var nodes = eastTreeObj.getCheckedNodes(true);
		// console.info(nodes);
		for (var i = 0; i < nodes.length; i++) {
			eastTreeObj.expandNode(nodes[i], true, true, true);
		}
	}
	function zTreeOnCheck(event, treeId, treeNode) {
		var eastTreeObj = $.fn.zTree.getZTreeObj("eastTree");
		eastTreeObj.expandNode(treeNode, true, true, true);
	}
	/* 角色树结束 */
};
$(document).ready(function() {
			$.fn.zTree.init($("#westTree"), setting);
		});
/* 菜单树结束 */

/* 初始化按钮 开始 */
$("#btu_add").button({
			icons : {
				primary : 'ui-icon-circle-plus'
			}
		}).click(function() {
			$(".left_menu").toggle();
		});
$(".left_menu").mouseleave(function() {
			$(".left_menu").hide();
		});
$("#btu_addt").button({
			icons : {
				primary : 'ui-icon-circle-plus'
			}
		}).click(function() {
	var treeObj = $.fn.zTree.getZTreeObj("westTree");
	var nodes = treeObj.getSelectedNodes();
	if (nodes.length <= 0) {
		$("#dialog_alert").empty();
		$("#dialog_alert").append("<label>请选中一个菜单节点!</label>");
		$("#dialog_alert").dialog({
					modal : true,
					buttons : {
						"确定" : function() {
							$(this).dialog("close");
						}
					}
				});
	} else {
		$("#a_cdmc").val("");
		$("#a_cdurl").val("");
		if (nodes[0].getParentNode() != null) {
			$("#a_pcdmc").val(nodes[0].getParentNode().text);
		}
		$("#dialog_add").dialog({
					modal : true,
					minWidth : 400,
					minHeight : 250,
					buttons : {
						"保存" : function() {
							$(this).dialog("close");
							$.post(baseUrl + "/cud/addMenu.do", {
										name : $("#a_cdmc").val(),
										url : $("#a_cdurl").val(),
										parent_id : nodes[0].pId
									}, function() {
										treeObj.reAsyncChildNodes(null,
												"refresh");
									});
						},
						"取消" : function() {
							$(this).dialog("close");

						}
					}
				});
	}
});
$("#btu_addx").button({
			icons : {
				primary : 'ui-icon-circle-plus'
			}
		}).click(function() {
	var treeObj = $.fn.zTree.getZTreeObj("westTree");
	var nodes = treeObj.getSelectedNodes();
	if (nodes.length <= 0) {
		$("#dialog_alert").empty();
		$("#dialog_alert").append("<label>请选中一个菜单节点!</label>");
		$("#dialog_alert").dialog({
					modal : true,
					buttons : {
						"确定" : function() {
							$(this).dialog("close");
						}
					}
				});
	} else {
		$("#a_cdmc").val("");
		$("#a_cdurl").val("");
		$("#a_pcdmc").val(nodes[0].text);
		$("#dialog_add").dialog({
					modal : true,
					minWidth : 400,
					minHeight : 250,
					buttons : {
						"保存" : function() {
							$(this).dialog("close");
							$.post(baseUrl + "/cud/addMenu.do", {
										name : $("#a_cdmc").val(),
										url : $("#a_cdurl").val(),
										parent_id : nodes[0].id
									}, function() {
										treeObj.reAsyncChildNodes(null,
												"refresh");
									});
						},
						"取消" : function() {
							$(this).dialog("close");

						}
					}
				});
	}
});
$("#btu_update").button({
			icons : {
				primary : 'ui-icon-circle-arrow-n'
			}
		}).click(function() {
	var treeObj = $.fn.zTree.getZTreeObj("westTree");
	var nodes = treeObj.getSelectedNodes();
	if (nodes.length <= 0) {
		$("#dialog_alert").empty();
		$("#dialog_alert").append("<label>请选中一个菜单节点!</label>");
		$("#dialog_alert").dialog({
					modal : true,
					buttons : {
						"确定" : function() {
							$(this).dialog("close");
						}
					}
				});
	} else {
		$("#u_cdmc").val(nodes[0].text);
		$("#u_cdurl").val(nodes[0].url);
		if (nodes[0].getParentNode() != null) {
			$("#u_pcdmc").val(nodes[0].getParentNode().text);
		}
		$("#dialog_update").dialog({
					modal : true,
					minWidth : 600,
					minHeight : 250,
					buttons : {
						"保存" : function() {
							$(this).dialog("close");
							var u_id = nodes[0].id;
							$.post(baseUrl + "/cud/updateMenu.do", {
										id : nodes[0].id,
										name : $("#u_cdmc").val(),
										url : $("#u_cdurl").val(),
										parent_id : nodes[0].pId
									}, function() {
										treeObj.reAsyncChildNodes(null,
												"refresh");
									});
						},
						"取消" : function() {
							$(this).dialog("close");

						}
					}
				});
	}
});
$("#btu_delete").button({
			icons : {
				primary : 'ui-icon-circle-minus'
			}
		}).click(function() {
	var treeObj = $.fn.zTree.getZTreeObj("westTree");
	var nodes = treeObj.getSelectedNodes();
	if (nodes.length <= 0) {
		$("#dialog_alert").empty();
		$("#dialog_alert").append("<label>请选中一个菜单节点!</label>");
		$("#dialog_alert").dialog({
					modal : true,
					buttons : {
						"确定" : function() {
							$(this).dialog("close");
						}
					}
				});
	} else {
		$("#dialog_alert").empty();
		$("#dialog_alert").append("<label>确定要删除[" + nodes[0].text
				+ "]节点吗?</label>");
		$("#dialog_alert").dialog({
					modal : true,
					buttons : {
						"确定" : function() {
							$.post(baseUrl + "/cud/deleteMenu.do", {
										id : nodes[0].id
									}, function(result) {
										treeObj.reAsyncChildNodes(null,
												"refresh");
									});
							$(this).dialog("close");
						},
						"取消" : function() {
							$(this).dialog("close");
						}
					}
				});
	}
});
$("#btu_save").button({
			icons : {
				primary : 'ui-icon-disk'
			}
		}).click(function() {
	var treeObj = $.fn.zTree.getZTreeObj("eastTree");
	var nodes = treeObj.getChangeCheckedNodes();
	if (nodes.length <= 0) {
		$("#dialog_alert").empty();
		$("#dialog_alert").append("<label>角色未改变,无需保存!</label>");
		$("#dialog_alert").dialog({
					modal : true,
					buttons : {
						"确定" : function() {
							$(this).dialog("close");
						}
					}
				});
	} else {
		var checkNodes = treeObj.getCheckedNodes(true);
		var menuRoleIds = checkNodes[0].id;
		var menuRoleNames = checkNodes[0].text;
		for (var i = 1; i < checkNodes.length; i++) {
			menuRoleIds = menuRoleIds + "," + checkNodes[i].id;
			menuRoleNames = menuRoleNames + "," + checkNodes[i].text;
		}
		var westTreeObj = $.fn.zTree.getZTreeObj("westTree");
		var westNodes = westTreeObj.getSelectedNodes();
		$.post(baseUrl + "/menu/saveMenuRole.do", {
					menuRoleIds : menuRoleIds,
					menuRoleNames : menuRoleNames,
					menuId : westNodes[0].id,
					menuName : westNodes[0].text,
					menuUrl : westNodes[0].url
				}, function(result) {
					if (result.success) {
						$("#dialog_alert").empty();
						$("#dialog_alert").append("<label>保存成功!</label>");
					} else {
						$("#dialog_alert").empty();
						$("#dialog_alert").append("<label>保存失败!</label>");
					}
					$("#dialog_alert").dialog({
								modal : true,
								buttons : {
									"确定" : function() {
										$(this).dialog("close");
										treeObj.reAsyncChildNodes(null,
												"refresh");
									}
								}
							});
				});
	}
});
/* 初始化按钮 结束 */

// 工具提示框 tooltip
$(document).tooltip();
